﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class WebUserControl_Thanggiangchuctuvanvien : System.Web.UI.UserControl
{
    string error = "";
    BCDataCenter.DataAccessLayer.DAL dt;
    static string oldsalelevelkey = "";
    static string user = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            dt = new BCDataCenter.DataAccessLayer.DAL();
            user = Session["Username"].ToString();
            imb_luu.Enabled = false;
            DataSet ds = new DataSet();
            ds = dt.MyExecuteReader(ref error, "P_GetLevel", CommandType.StoredProcedure);
            try
            {
                if (ds.Tables[0].Rows.Count >= 1)
                {
                    dr_chucdanhmoi.DataSource = ds.Tables[0];
                    dr_chucdanhmoi.DataValueField = "levelid";
                    dr_chucdanhmoi.DataTextField = "levelname";
                    dr_chucdanhmoi.DataBind();
                }
            }
            catch { return; }
            ds.Dispose();
        }
    }
    protected void txt_ma_TextChanged(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        dt = new BCDataCenter.DataAccessLayer.DAL();
        ds = dt.MyExecuteQuery(ref error, "P_Getsalesp", CommandType.StoredProcedure, new SqlParameter("@salespkey", txt_ma.Text.ToString()));
        try
        {
            if (ds.Tables[0].Rows.Count >= 1)
            {
                txt_ma.Text = ds.Tables[0].Rows[0]["salespkey"].ToString();
                lbl_ten.Text = ds.Tables[0].Rows[0]["salespname"].ToString();
                lbl_diachi.Text = ds.Tables[0].Rows[0]["address"].ToString();
                lbl_dienthoai.Text = ds.Tables[0].Rows[0]["phone"].ToString();
                lbl_chucdanhcu.Text = ds.Tables[0].Rows[0]["oldlevel"].ToString();
                oldsalelevelkey = ds.Tables[0].Rows[0]["oldlevelkey"].ToString();
                lbl_nguothuchien.Text = user;
                imb_luu.Enabled = true;
            }
            else
            {
                //txt_ma.Text = "";
                uscMsgBox1.AddMessage("không có tư vấn viên này", uscMsgBox.enmMessageType.Error);
            }
        }
        catch { return; }
    }
    protected void imb_taomoi_Click(object sender, ImageClickEventArgs e)
    {
        txt_ma.Text = "";
        lbl_ten.Text = "";
        lbl_diachi.Text = "";
        lbl_dienthoai.Text = "";
        lbl_chucdanhcu.Text = "";
        txt_refno.Text = "";
        txt_AproveBy.Text = "";
        txt_ma.ReadOnly = false;
        imb_luu.Enabled = false;
    }
    protected void imb_luu_Click(object sender, ImageClickEventArgs e)
    {
        DateTime dates = DateTime.Today;
        string date = dates.ToString();
        dt = new BCDataCenter.DataAccessLayer.DAL();
        if (txt_AproveBy.Text == "" || txt_refno.Text == "")
            uscMsgBox1.AddMessage("Không được để trống những ô có dấu *", uscMsgBox.enmMessageType.Error);
        else
        {
            try
            {
                int autopromote = 1;
                    if (CheckBox1.Checked == false)
                        autopromote = 0;
                int newlevel = int.Parse(dr_chucdanhmoi.SelectedValue.ToString());
                string salespkey = txt_ma.Text.ToString();
                bool kq = false;
                //kq = dt.MyExecuteNonQuerys("INSERT INTO HR_ManuallyPromoteSalesp (Salespkey, oldsaleslevel, newsaleslevel, AproveBy, RefNo, RecUser, RecTime) VALUES     ('" + txt_ma.Text.ToString() + "'," + int.Parse(oldsalelevelkey) + "," + int.Parse(dr_chucdanhmoi.SelectedValue.ToString()) + ",'" + txt_AproveBy.Text.ToString() + "','" + txt_refno.Text.ToString() + "','" + user + "','" + date + ")", CommandType.Text, ref error);
                kq = dt.MyExecuteNonQuery("P_Insert_HRManualPrS", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@salespkey", salespkey),
                    new SqlParameter("@oldsalelevel", int.Parse(oldsalelevelkey)),
                    new SqlParameter("@newsalelevel",newlevel),
                    new SqlParameter("@AproveBy", txt_AproveBy.Text.ToString()),
                    new SqlParameter("@RecUser", user),
                    new SqlParameter("@RefNo", txt_refno.Text.ToString()),
                    new SqlParameter("@RecTime", date),
                    new SqlParameter("@lockautopromote",autopromote));                
                if (kq)
                {
                    
                    kq = dt.MyExecuteNonQuery(String.Format("UPDATE    ARSALESP SET SalesLevel = {0}, lockautopromote = {1} WHERE salesp_key = '{2}'", newlevel, autopromote, salespkey), CommandType.Text, ref error);
                    //imb_luu.Enabled = false;
                    if(kq)
                    uscMsgBox1.AddMessage("Hoàn thành cập nhật dữ liệu", uscMsgBox.enmMessageType.Success);
                    txt_ma.ReadOnly = true;
                }
            }
            catch (Exception ex)
            {
                uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
            }
        }
    }
}
